home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Draw / Sources / LineShp.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  10.4 KB  |  394 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                LineShp.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef BASESHP_H
  15. #include "BaseShp.h"
  16. #endif
  17.  
  18. #ifndef LINESHP_H
  19. #include "LineShp.h"
  20. #endif
  21.  
  22. #ifndef UTILS_H
  23. #include "Utils.h"
  24. #endif
  25.  
  26. #ifndef CONSTANT_H
  27. #include "Constant.h"
  28. #endif
  29.  
  30. #ifndef DRAWPART_H
  31. #include "DrawPart.h"
  32. #endif
  33.  
  34. #ifndef DRAWPART_H
  35. #include "DrawPart.h"
  36. #endif
  37.  
  38. #ifndef DRAWFRM_H
  39. #include "DrawFrm.h"
  40. #endif
  41.  
  42. #ifndef DRAWPRXY_H
  43. #include "DrawPrxy.h"
  44. #endif
  45.  
  46. #ifndef DRAWLINK_H
  47. #include "DrawLink.h"
  48. #endif
  49.  
  50. #ifndef DRAWCLIP_H
  51. #include "DrawClip.h"
  52. #endif
  53.  
  54. // ----- Part Layer -----
  55.  
  56. #ifndef FWFRMING_H
  57. #include "FWFrming.h"
  58. #endif
  59.  
  60. #ifndef FWUTIL_H
  61. #include "FWUtil.h"
  62. #endif
  63.  
  64. #ifndef FWSELECT_H
  65. #include "FWSelect.h"
  66. #endif
  67.  
  68. #ifndef FWITERS_H
  69. #include "FWIters.h"
  70. #endif
  71.  
  72. // ----- OS Layer -----
  73.  
  74. #ifndef FWEVENT_H
  75. #include "FWEvent.h"
  76. #endif
  77.  
  78. #ifndef FWRECSHP_H
  79. #include "FWRecShp.h"
  80. #endif
  81.  
  82. #ifndef FWTXTBOX_H
  83. #include "FWTxtBox.h"
  84. #endif
  85.  
  86. #ifndef FWLINSHP_H
  87. #include "FWLinShp.h"
  88. #endif
  89.  
  90. #ifndef FWOVLSHP_H
  91. #include "FWOvlShp.h"
  92. #endif
  93.  
  94. #ifndef FWRRCSHP_H
  95. #include "FWRRcShp.h"
  96. #endif
  97.  
  98. #ifndef FWODGEOM_H
  99. #include "FWODGeom.h"
  100. #endif
  101.  
  102. #ifndef FWGRUTIL_H
  103. #include "FWGrUtil.h"
  104. #endif
  105.  
  106. // ----- Foundation Includes -----
  107.  
  108. #ifndef FWSTREAM_H
  109. #include "FWStream.h"
  110. #endif
  111.  
  112. // ----- OpenDoc Includes -----
  113.  
  114. #ifndef SOM_ODTransform_xh
  115. #include <Trnsform.xh>
  116. #endif
  117.  
  118. //========================================================================================
  119. // Runtime Information
  120. //========================================================================================
  121.  
  122. #ifdef FW_BUILD_MAC
  123. #pragma segment odfdrawshapes
  124. #endif
  125.  
  126. //========================================================================================
  127. // RunTime Info
  128. //========================================================================================
  129.  
  130. FW_DEFINE_AUTO(CLineShape)
  131. FW_DEFINE_CLASS_M1(CLineShape, CBaseShape)
  132.  
  133. const FW_ClassTypeConstant LLineShape = FW_TYPE_CONSTANT('s','h','l','n');
  134. FW_REGISTER_ARCHIVABLE_CLASS(LLineShape, CLineShape, CLineShape::Read, 0, 0, CBaseShape::Write)
  135.  
  136. //========================================================================================
  137. // class CLineShape
  138. //========================================================================================
  139.  
  140. //----------------------------------------------------------------------------------------
  141. // CLineShape::CLineShape
  142. //----------------------------------------------------------------------------------------
  143.  
  144. CLineShape::CLineShape(CDrawPart* drawPart) :
  145.     CBaseShape(drawPart, 2, kLineShape, kFrameOnly)
  146. {
  147.     FW_END_CONSTRUCTOR
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // CLineShape::CLineShape
  152. //----------------------------------------------------------------------------------------
  153.  
  154. CLineShape::CLineShape(CDrawPart* drawPart, FW_CReadableStream& archive) :
  155.     CBaseShape(drawPart, archive)
  156. {    
  157.     archive >> fStart;
  158.     archive >> fEnd;
  159.  
  160.     FW_END_CONSTRUCTOR
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // CLineShape::~CLineShape
  165. //----------------------------------------------------------------------------------------
  166.  
  167. CLineShape::~CLineShape()
  168. {
  169.     FW_START_DESTRUCTOR
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. // CLineShape::RenderShape
  174. //----------------------------------------------------------------------------------------
  175.  
  176. void CLineShape::RenderShape(Environment* ev, ODFacet* facet, FW_CGraphicContext& gc)
  177. {    
  178. FW_UNUSED(ev);
  179. FW_UNUSED(facet);
  180.     FW_CLineShape::RenderLine(gc, fStart, fEnd, fFrameInk, fFrameStyle);        
  181. }
  182.  
  183. //----------------------------------------------------------------------------------------
  184. // CLineShape::OutlineShape
  185. //----------------------------------------------------------------------------------------
  186.  
  187. void CLineShape::OutlineShape(FW_CGraphicContext& gc, const FW_CInk& ink, const FW_CStyle& style, 
  188.                               const FW_CPoint& pt1, const FW_CPoint& pt2)
  189. {
  190.     FW_CLineShape::RenderLine(gc, pt1, pt2, ink, style);        
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // CLineShape::HitTest
  195. //----------------------------------------------------------------------------------------
  196.  
  197. FW_Boolean CLineShape::HitTest(Environment *ev, FW_CGraphicContext& gc, const FW_CMouseEvent& theMouseEvent) const
  198. {
  199.     // We should convert the point to shape coordinate
  200.     FW_CPoint mouse = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  201.     ODFacet* facet = theMouseEvent.GetFacet(ev);
  202.     FW_CFrame* frame = FW_CFrame::ODtoFWFrame(ev, facet->GetFrame(ev));
  203.     frame->GetContentView(ev)->FrameToViewContent(ev, mouse);
  204.  
  205.     FW_CLineShape lineShape(fStart.x, fStart.y, fEnd.x, fEnd.y);
  206.     return lineShape.HitTest(gc, mouse, FW_IntToFixed(2));
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // CLineShape::CalcClipShape
  211. //----------------------------------------------------------------------------------------
  212.  
  213. ODShape* CLineShape::CalcClipShape(Environment* ev)
  214. {
  215.     return ::FW_CreateLineODShape(ev, fStart, fEnd, GetPenSize());
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // CLineShape::CreateShapeOutline
  220. //----------------------------------------------------------------------------------------
  221.  
  222. ODShape* CLineShape::CreateShapeOutline(Environment *ev)
  223. {
  224.     return FW_CopyAndRelease(ev, AcquireClipShape(ev));
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // CLineShape::GetHandleCenter
  229. //----------------------------------------------------------------------------------------
  230.  
  231. void CLineShape::GetHandleCenter(short whichHandle, FW_CPoint& center) const
  232. {
  233.     center = whichHandle == kInTopLeftCorner ? fStart : fEnd;
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // CLineShape::ResizeFeedback
  238. //----------------------------------------------------------------------------------------
  239.  
  240. void CLineShape::ResizeFeedback(FW_CGraphicContext& gc, const FW_CInk& ink, const FW_CStyle& style, 
  241.                                 short whichHandle, const FW_CPoint& mouseLoc)
  242. {
  243.     FW_CRect srcRect, dstRect;
  244.     GetMapRects(whichHandle, mouseLoc, srcRect, dstRect);
  245.     
  246.     FW_CPoint pt1(fStart);
  247.     FW_CPoint pt2(fEnd);
  248.  
  249.     pt1.Map(srcRect, dstRect);
  250.     pt2.Map(srcRect, dstRect);
  251.  
  252.     if (srcRect.left == srcRect.right)
  253.     {
  254.         pt1.x = dstRect.left;
  255.         pt2.x = dstRect.right;
  256.     }
  257.     
  258.     if (srcRect.top == srcRect.bottom)
  259.     {
  260.         pt1.y = dstRect.top;
  261.         pt2.y = dstRect.bottom;
  262.     }
  263.     
  264.     OutlineShape(gc, ink, style, pt1, pt2);
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // CLineShape::GetMapRects
  269. //----------------------------------------------------------------------------------------
  270.  
  271. void CLineShape::GetMapRects(short whichHandle, const FW_CPoint& mouseLoc,
  272.                                 FW_CRect& srcRect, FW_CRect& dstRect)
  273. {
  274.     srcRect.Set(fStart.x, fStart.y, fEnd.x, fEnd.y);
  275.     dstRect = srcRect;
  276.  
  277.     switch (whichHandle)
  278.     {
  279.         case kInTopLeftCorner:
  280.             dstRect.left = mouseLoc.x;
  281.             dstRect.top = mouseLoc.y;
  282.             break;
  283.         case kInBottomRightCorner:
  284.             dstRect.right = mouseLoc.x;
  285.             dstRect.bottom = mouseLoc.y;
  286.             break;
  287.     }
  288. }
  289.  
  290. //----------------------------------------------------------------------------------------
  291. // CLineShape::MapShape
  292. //----------------------------------------------------------------------------------------
  293.  
  294. void CLineShape::MapShape(Environment *ev, const FW_CRect& srcRect, const FW_CRect& dstRect)
  295. {
  296. FW_UNUSED(srcRect);
  297.  
  298.     CheckPromise(ev);
  299.     
  300.     ClearCache(ev);
  301.  
  302.     FW_CRect realSrcRect;
  303.     realSrcRect[FW_kTopLeft] = fStart;
  304.     realSrcRect[FW_kBotRight] = fEnd;
  305.  
  306.     fStart.Map(realSrcRect, dstRect);
  307.     fEnd.Map(realSrcRect, dstRect);
  308.  
  309.     if (realSrcRect.left == realSrcRect.right)
  310.     {
  311.         fStart.x = dstRect.left;
  312.         fEnd.x = dstRect.right;
  313.     }
  314.     
  315.     if (realSrcRect.top == realSrcRect.bottom)
  316.     {
  317.         fStart.y = dstRect.top;
  318.         fEnd.y = dstRect.bottom;
  319.     }
  320. }
  321.  
  322. //----------------------------------------------------------------------------------------
  323. // CLineShape::OffsetShape
  324. //----------------------------------------------------------------------------------------
  325.  
  326. void CLineShape::OffsetShape(Environment *ev, FW_Fixed xDelta, FW_Fixed yDelta)
  327. {
  328. FW_UNUSED(ev);
  329. //    CheckPromise(ev); [HLX] Do not call CheckPromise from OffsetShape or end up in a infinity loop
  330. //                        because externalize of a pict calls OffsetShape
  331.     ClearCache(ev);
  332.  
  333.     fStart.x += xDelta;
  334.     fStart.y += yDelta;
  335.  
  336.     fEnd.x += xDelta;
  337.     fEnd.y += yDelta;
  338. }
  339.  
  340. //----------------------------------------------------------------------------------------
  341. // CLineShape::SetShapeGeometry
  342. //----------------------------------------------------------------------------------------
  343.  
  344. void CLineShape::SetShapeGeometry(const FW_CPoint& anchorPoint,  const FW_CPoint& currentPoint)
  345. {
  346.     fStart = anchorPoint;
  347.     fEnd = currentPoint;
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. // CLineShape::Flatten
  352. //----------------------------------------------------------------------------------------
  353.  
  354. void CLineShape::Flatten(FW_CWritableStream& archive)
  355. {    
  356.     CBaseShape::Flatten(archive);
  357.     
  358.     archive << fStart;
  359.     archive << fEnd;
  360. }
  361.  
  362. //----------------------------------------------------------------------------------------
  363. // CLineShape::Read
  364. //----------------------------------------------------------------------------------------
  365.  
  366. void* CLineShape::Read(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  367. {
  368. FW_UNUSED(type);
  369.     // [HLX] This is a hack until I can register object with the archiver
  370.     CDrawReadableStream *drawArchive = (CDrawReadableStream*)&stream;
  371.     return FW_NEW(CLineShape, (drawArchive->GetDrawPart(), stream));
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. // CLineShape::GetObjectClass
  376. //----------------------------------------------------------------------------------------
  377.  
  378. ODDescType CLineShape::GetObjectClass() const
  379. {
  380.     return cLine;
  381. }
  382.  
  383. //----------------------------------------------------------------------------------------
  384. // CLineShape::GetRectGeometry
  385. //----------------------------------------------------------------------------------------
  386.  
  387. FW_CRect CLineShape::GetRectGeometry() const
  388. {
  389.     FW_CRect rect(fStart, fEnd);
  390.     rect.Sort();
  391.     return rect;
  392. }
  393.  
  394.